package com.greenant.store.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.avos.avoscloud.AVAnalytics;
import com.greenant.store.R;
import com.greenant.store.utils.Const;
import com.greenant.store.version.DownloadManager;
import com.lsarah.utils.SystemBarTintManager;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends ActionBarActivity implements OnItemClickListener{

	GridView gv=null;
	ImageView iset=null,imsg=null;
	
	int[] ids ={R.drawable.g1,R.drawable.g2,R.drawable.g3,
			R.drawable.g4,R.drawable.g6};
	String[] intentInfo = {
			"com.example.mystore.MyStoreActivity",
			"com.example.mystore.MyStoreActivity",
			"",
			"",
			"",
			""
	};
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
//        setContentView(R.layout.activity_main);    
        
    	SystemBarTintManager.setContentView(this,
				R.layout.activity_main,
				getResources().getColor(R.color.firebrick));
        AVAnalytics.trackAppOpened(getIntent());

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
                       
        GridView gv=(GridView)findViewById(R.id.maingrid);
        ImageView iset=(ImageView)findViewById(R.id.setting);
        ImageView imsg=(ImageView)findViewById(R.id.msg);
                     
        ArrayList<HashMap<String,Object>> al=new ArrayList<HashMap<String,Object>>();
        
        for(int i=0;i<ids.length;i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>(); 
                
            map.put("icon",ids[i]);//���ͼ����Դ��ID 
            al.add(map); 
        }
         
        SimpleAdapter sa=new SimpleAdapter(this,al,R.layout.main_grid_item,new String[]{"icon"},new int[]{R.id.ItemImage});
        gv.setAdapter(sa);
        gv.setOnItemClickListener(new OnItemClickListener() 
        { 
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
            { 
             //   Toast.makeText(MainActivity.this, "pic" + position, Toast.LENGTH_SHORT).show(); 
            	
               Intent intent = new Intent();//"android.intent.action.MYSTORE");
              	
               switch(position)
               {
				case 0://店铺
				   intent.setClass(MainActivity.this,MyStoreActivity.class);
				   startActivity(intent);
				   break;
				case 1://商品
					   intent.setClass(MainActivity.this,MyGoodsActivity.class);
					   startActivity(intent);
					   break;
				case 2://订单
				   intent.setClass(MainActivity.this,MyOrderActivity.class);
				   startActivity(intent);
				   break;
				case 3://快递
					   intent.setClass(MainActivity.this,ExpressManageActivity.class);
					   startActivity(intent);
					   break;
//				case 4://销售管理
//					 intent = new Intent(MainActivity.this, CaptureActivity.class);
//					startActivity(intent);
//					   break;
				case 4://收入管理
					   intent.setClass(MainActivity.this,MyIncomeActivity.class);
					   startActivity(intent);
					   break;
            	default:
            	   break;
               }            
               
            } 
        }); 
        gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        
        iset.setOnClickListener(new OnClickListener() 
        { 
        	@Override
			public void onClick(View arg0) {
        		//设置界面
              Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
              startActivity(intent);	
			} 
        }); 
        imsg.setOnClickListener(new OnClickListener() 
        { 
        	@Override
			public void onClick(View v) {
				//消息通知界面
        		Intent intent = new Intent(MainActivity.this, MyMsgActivity.class);
                startActivity(intent);	
			} 
        }); 
        
        Const.mMainActivity = this;
        
      //检查更新
        DownloadManager downloadManager=new DownloadManager(this,false,true);
        downloadManager.checkDownload();
    }
    
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
         
     //   Intent intent =new Intent(this,AboutHeroActivty.class);
      //  intent.putExtra("HERO", heros[position]);
       // this.startActivity(intent);
         
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }


}
